package leetcode.number;

public class MaxSubArray {

    public static int maxSubArray(int[] nums) {

//         int pre = 0, maxAns = nums[0];
//         for (int x : nums) {
//             pre = Math.max(pre + x, x);
//             maxAns = Math.max(maxAns, pre);
//         }
//         return maxAns;

        int result = nums[0];
        int head = 0;
        int tail = 0;
        int num = 0;
        if (nums != null && nums.length > 1){
            while (true){
                if (head == nums.length-1){
                    break;
                }

                if (tail == nums.length){
                    head++;
                    tail = head;
                    num = 0;
                }

                num += nums[tail];
                if (num > result){
                    result = num;
                }

                tail++;
            }
        }
        return result;
    }

}
